//
//  FIShowAlert.m
//  Huitang
//
//  Created by zou shigang on 14/7/1.
//  Copyright (c) 2014年 LC_WORLD. All rights reserved.
//

#import "FIShowAlert.h"

@interface FNActivityIndicatorView2 : UIActivityIndicatorView
{
	int animationCount;
}
@property(nonatomic,assign) int animationCount;

@end


@implementation FNActivityIndicatorView2
@synthesize animationCount;

-(void) startAnimating{
	if(animationCount<1){
		[super startAnimating];
	}
	++animationCount;
}

-(void) stopAnimating{
	--animationCount;
    animationCount=0;
	if(animationCount<1){
		[super stopAnimating];
	}
}

@end


const int YK_TAG_AIV=4924373;	//loading
const int YK_TAG_AIV_MASK=4924374;
@implementation UIView(add_loading_animation)

#pragma mark loading animation

-(void) startLoadingAnimation
{
	FNActivityIndicatorView2* aiv=(FNActivityIndicatorView2*)[self viewWithTag:YK_TAG_AIV];
	if(aiv!=nil){
		[aiv startAnimating];
		return;
	}
	
	UIView* aivMaskView=[self viewWithTag:YK_TAG_AIV_MASK];
	if(aivMaskView!=nil){
		return;
	}
	aivMaskView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
	//aivMaskView.center=self.center;
    aivMaskView.frame=CGRectMake((self.frame.size.width-60)/2, (self.frame.size.height-60)/2, 60, 60);
	aivMaskView.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin ;
	aivMaskView.backgroundColor=[UIColor grayColor];
	aivMaskView.layer.cornerRadius=10;
	aivMaskView.alpha=0.5;
	aivMaskView.tag=YK_TAG_AIV_MASK;
	[self addSubview:aivMaskView];
	
	aiv=[[FNActivityIndicatorView2 alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
	aiv.tag=YK_TAG_AIV;
	//aiv.center=self.center;
    aiv.frame=CGRectMake((self.frame.size.width-32)/2, (self.frame.size.height-32)/2, 32, 32);
	aiv.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin| UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
	[self addSubview:aiv];
    
    //self.userInteractionEnabled = NO;
	[aiv startAnimating];
}

- (void)stopLoadingAnimation
{
	FNActivityIndicatorView2* aiv=(FNActivityIndicatorView2*)[self viewWithTag:YK_TAG_AIV];
    //self.userInteractionEnabled = YES;
	[aiv stopAnimating];
	if([aiv animationCount]>0){
		return;
	}
	[aiv removeFromSuperview];
    
	UIView* aivMaskView=[self viewWithTag:YK_TAG_AIV_MASK];
	[aivMaskView removeFromSuperview];
}

@end


//base alert
#import <QuartzCore/QuartzCore.h>
#import <AudioToolbox/AudioToolbox.h>

#define BaseAlertLabelViewTag 11
#define BG_TAG 121

static BaseAlertLabelView *alertView = nil;
static BaseAlertLabelView *alertBaseView = nil;
static UILocalNotification *notification = nil;

#define SOUNDID  1109  //1012 -iphone   1152 ipad  1109 ipad
//base alert view for app
@implementation BaseAlertLabelView

@synthesize timer;

+ (void)playSound:(BOOL)success;
{
    if (success) {
        AudioServicesPlaySystemSound(SOUNDID);
    } else {
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);   //让手机震动
    }
}

+ (void)CancelLocalInfo:(NSString*)info
{
    //获得 UIApplication
    UIApplication *app = [UIApplication sharedApplication];
    //获取本地推送数组
    NSArray *localArray = [app scheduledLocalNotifications];
    //声明本地通知对象
    UILocalNotification * localNotification;
    if (localArray) {
        for (UILocalNotification *noti in localArray) {
            NSInteger activityId = [[noti.userInfo objectForKey:@"activityid"] intValue];
            if (activityId==9527) {
                [app cancelLocalNotification:localNotification];
            }
        }
    }
}

+ (void)PostLocalInfo:(NSString*)info
{
    //本地通知
    if (notification==nil) {
        notification = [[UILocalNotification alloc] init];
    }
    
    NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:5];
    if (notification != nil) {
        // 设置推送时间
        notification.fireDate = pushDate;
        // 设置时区
        notification.timeZone = [NSTimeZone defaultTimeZone];
        // 设置重复间隔
        notification.repeatInterval = kCFCalendarUnitEra;
        // 推送声音
        notification.soundName = @"dp3.caf";//UILocalNotificationDefaultSoundName;
        // 推送内容
        notification.alertBody = info;
        //显示在icon上的红色圈中的数子
        notification.applicationIconBadgeNumber = 1;
        //设置userinfo 方便在之后需要撤销的时候使用
        NSDictionary *info = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:9527] forKey:@"key"];
        notification.userInfo = info;
        //添加推送到UIApplication
        UIApplication *app = [UIApplication sharedApplication];
        [app scheduleLocalNotification:notification];
    }
}

+ (BaseAlertLabelView *)getAlertLabelView:(UIView*)baseview
{
    CGRect frame;
    if (alertBaseView == nil) {
        frame=baseview.bounds;//CGRectMake(0, 0, 150, 150);
        alertBaseView = [[self alloc] initWithFrame:frame withBaseView:baseview];
        alertBaseView.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
    }
    [baseview addSubview:alertBaseView];
    [baseview bringSubviewToFront:alertBaseView];
    
    return alertBaseView;
}

+ (BaseAlertLabelView *)getAlertLabelView {
    CGRect frame;
    if (alertView == nil) {
        frame=CGRectMake(0, 0, 150, 150);
        alertView = [[self alloc] initWithFrame:frame];
        alertView.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
    }
    return alertView;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        if ([self.layer respondsToSelector: @selector(setCornerRadius:)]) {
            [self.layer setCornerRadius:10];
        }
        self.tag = BaseAlertLabelViewTag;
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8];
        alertLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 50+10, frame.size.width-20, 50)];
        alertLabel.numberOfLines = 0;
        alertLabel.backgroundColor = [UIColor clearColor];
        alertLabel.textColor = [UIColor whiteColor];
        alertLabel.textAlignment = NSTextAlignmentCenter;
        alertLabel.font = [UIFont boldSystemFontOfSize:15];
        alertLabel.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        [self addSubview:alertLabel];
        
        alertLabel_Title = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, frame.size.width, 30)];
        alertLabel_Title.numberOfLines = 0;
        alertLabel_Title.backgroundColor = [UIColor clearColor];
        alertLabel_Title.textColor = [UIColor whiteColor];
        alertLabel_Title.textAlignment = NSTextAlignmentCenter;
        alertLabel_Title.font = [UIFont boldSystemFontOfSize:16];
        alertLabel_Title.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        [self addSubview:alertLabel_Title];
        
        self.hidden = YES;
        [[[UIApplication sharedApplication] keyWindow].rootViewController.view addSubview:self];
        self.center = [[UIApplication sharedApplication] keyWindow].rootViewController.view.center;
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame withBaseView:(UIView*)baseview
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor=[UIColor clearColor];
        
        UIView * beside_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
        beside_view.center=self.center;
        beside_view.backgroundColor=[UIColor clearColor];
        // Initialization code
        if ([beside_view.layer respondsToSelector: @selector(setCornerRadius:)]) {
            [beside_view.layer setCornerRadius:10];
        }
        
        self.tag = BaseAlertLabelViewTag;
        beside_view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8];
        alertLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 35, 110, 110)];
        alertLabel.numberOfLines = 0;
        alertLabel.backgroundColor = [UIColor clearColor];
        alertLabel.textColor = [UIColor whiteColor];
        alertLabel.textAlignment = NSTextAlignmentCenter;
        alertLabel.font = [UIFont boldSystemFontOfSize:15];
        alertLabel.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        [beside_view addSubview:alertLabel];
        self.hidden = YES;
        //添加到baseview中
        //        [baseview addSubview:self];
        //        [baseview bringSubviewToFront:self];
        activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        activity.frame=CGRectMake((150-32)/2, 30, 32, 32);
        [beside_view addSubview:activity];
        
        alertLabel_Title = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 150, 30)];
        alertLabel_Title.numberOfLines = 0;
        alertLabel_Title.backgroundColor = [UIColor clearColor];
        alertLabel_Title.textColor = [UIColor whiteColor];
        alertLabel_Title.textAlignment = NSTextAlignmentCenter;
        alertLabel_Title.font = [UIFont boldSystemFontOfSize:16];
        alertLabel_Title.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        alertLabel_Title.hidden=YES;
        [beside_view addSubview:alertLabel_Title];
        
        [self addSubview:beside_view];
    }
    return self;
}

//- (void)fullScreenPlayerShowNotification {
//    self.hidden = YES;
//    if (self.timer) {
//        [timer invalidate];
//        self.timer = nil;
//    }
//}

+ (void)stopShowMsg
{
    if (alertBaseView!=nil) {
        [alertBaseView stopActivity];
        [alertBaseView removeFromOutManView];
    }
}

- (void)stopActivity
{
    if (activity) {
        [activity stopAnimating];
    }
}

- (void)showMsg:(NSString *)string
{
    if (string == nil) {
        return;
    }
    alertLabel.text = string;
    if (activity) {
        [activity startAnimating];
        activity.hidden=NO;
    }
    self.hidden = NO;
    if (alertLabel_Title) {
        alertLabel_Title.hidden=YES;
    }
    if (self.timer) {
        [timer invalidate];
        self.timer = nil;
    }
}

- (void)showBaseViewWithString:(NSString *)string
{
    if (string == nil) {
        return;
    }
    alertLabel.text = string;
    
    self.hidden = NO;
    if (self.timer) {
        [timer invalidate];
        self.timer = nil;
    }
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                  target:self
                                                selector:@selector(removeFromOutManView)
                                                userInfo:nil
                                                 repeats:NO];
}

- (void)showWithString:(NSString *)string andTitle:(NSString*)title
{
    if (string == nil) {
        return;
    }
    if (alertLabel_Title) {
        alertLabel_Title.text=title;
        alertLabel_Title.hidden=NO;
    }
    
    alertLabel.text = string;
    self.hidden = NO;
    
    if (activity) {
        [activity stopAnimating];
        activity.hidden=YES;
    }
    
    [[[UIApplication sharedApplication] keyWindow].rootViewController.view bringSubviewToFront:self];
    if (self.timer) {
        [timer invalidate];
        self.timer = nil;
    }
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(removeSelf) userInfo:nil repeats:NO];
}

- (void)showWithString:(NSString *)string {
    if (string == nil) {
        return;
    }
    alertLabel_Title.text=@"";
    alertLabel.text = string;
    self.hidden = NO;
    [[[UIApplication sharedApplication] keyWindow].rootViewController.view bringSubviewToFront:self];
    if (self.timer) {
        [timer invalidate];
        self.timer = nil;
    }
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(removeSelf) userInfo:nil repeats:NO];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    [self removeFromSuperview];
}

- (void)removeFromOutManView {
    [UIView animateWithDuration:0.5 animations:^{
        self.hidden=YES;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)removeSelf {
    CATransition *myTranstiton = [CATransition animation];
    myTranstiton.duration = 0.5;
    myTranstiton.type = kCATransitionFade;
    self.hidden = YES;
    [self.layer addAnimation:myTranstiton forKey:nil];
}

- (void)dealloc {
    
}

@end
